﻿using System.Windows.Forms;

namespace Outlook.WorkItems.Extension.Controls
{
    /// <summary>
    /// Represents a treeview node of type File in a document librairy
    /// </summary>
    public class FileNode : TreeNode
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="text">Node Text</param>
        /// <param name="path">File Path</param>
        /// <param name="imageList">Image List of treeview</param>
        public FileNode(string text, string path, ImageList imageList)
            : base(text)
        {
            Tag = path;
            string extension = text.Substring(text.LastIndexOf('.'), text.Length - text.LastIndexOf('.'));
            ImageIndex = IconsCache.GetIconIndex(imageList, extension);
            SelectedImageIndex = ImageIndex;
        }

        /// <summary>
        /// Returns parent node of type Team Server Node
        /// </summary>
        public TeamServerNode TeamServerNode
        {
            get
            {
                TreeNode currentNode = this;
                while (!(currentNode is TeamServerNode) && currentNode != null)
                {
                    currentNode = currentNode.Parent;
                }
                return currentNode as TeamServerNode;
            }
        }

        /// <summary>
        /// Returns parent node of type Document librairy Node
        /// </summary>
        public DocumentLibrairyNode DocumentLibrairyNode
        {
            get
            {
                TreeNode currentNode = this;
                while (!(currentNode is DocumentLibrairyNode) && currentNode != null)
                {
                    currentNode = currentNode.Parent;
                }
                return currentNode as DocumentLibrairyNode;
            }
        }

        /// <summary>
        /// Returns parent node of type Team Project Node
        /// </summary>
        public TeamProjectNode TeamProjectNode
        {
            get
            {
                TreeNode currentNode = this;
                while (!(currentNode is TeamProjectNode) && currentNode != null)
                {
                    currentNode = currentNode.Parent;
                }
                return currentNode as TeamProjectNode;
            }
        }

        /// <summary>
        /// Return file path
        /// </summary>
        public string FilePath
        {
            get
            {
                //string path = string.Format("{0}/{1}", this.TeamProjectNode.Tag.ToString(), this.Tag.ToString());

                return Tag.ToString();

            }
        }
    }
}
